package com.lgq.cfw.controller.permission;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgq.cfw.bean.TablePermission;
import com.lgq.cfw.bean.TableUser;
import com.lgq.cfw.bean.message.Message;
import com.lgq.cfw.service.UserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Lin
 * @date 2019-02-04 15:53
 **/
@RestController
@RequestMapping(value ="/permission/user")
public class UserController {

  @Autowired
  UserService userService;

  /**
   * 用户注册
   * @param tableUser 前端传来的用户信息的封装
   * @return Ajax Json数据
   */
  @RequestMapping(value = "/register",method = RequestMethod.POST)
  public Message register(TableUser tableUser,HttpServletRequest httpServletRequest){
    //判断是否注册成功
    boolean flag=userService.register(tableUser);
    if (flag){
      httpServletRequest.getSession().setAttribute("loginName",tableUser.getLoginacct());
      return Message.success("用户注册成功",null);
    }else {
      return Message.fail("该账号已存在",null);
    }
  }

  /**
   * 通过id获取用户信息
   * @param userId id
   * @return tableUser
   */
  @RequestMapping(value = "/getUser",method = RequestMethod.GET)
  public Message getUserById(@RequestParam(value = "userId") Integer userId){
    TableUser tableUser=userService.getUserById(userId);
    return Message.success("处理成功",null).add("user",tableUser);
  }

  /**
   * 检查是否存在账号
   * @param loginacct 用户输入的账号
   * @return Ajax Json消息
   */
  @RequestMapping(value = "/checkAccount",method = RequestMethod.POST)
  public Message checkAccount(String loginacct){
    //判断是否已经存在该账号
    boolean flag=userService.checkAccount(loginacct);
    if (flag){
      return Message.success("该账号可以使用",null);
    }else {
      return Message.fail("该账号已存在",null);
    }
  }


  /**
   * 登录
   * @param loginacct 账号
   * @param userpswd 密码
   * @param httpServletRequest 负责将用户名存入session中
   * @return json数据
   */
  @RequestMapping(value = "/login",method = RequestMethod.POST)
  public Message login(String loginacct,String userpswd, HttpServletRequest httpServletRequest){
    boolean flag=userService.login(loginacct,userpswd);
    if (flag){
      return Message.fail("登录失败",null);
    }else{
      //登录成功放入session域中
      httpServletRequest.getSession().setAttribute("loginName",loginacct);
      return Message.success("登录成功",null);
    }
  }


  /**
   * 从session域中取出用户名
   * @param httpServletRequest  取出session
   * @return json数据
   */
  @RequestMapping(value = "/getName",method = RequestMethod.POST)
  public Message getName(HttpServletRequest httpServletRequest){
    //获取Session域中的loginName值
    String loginName=(String)httpServletRequest.getSession().getAttribute("loginName");
    //是否返回为空
    if (userService.getName(loginName)){
      return Message.success("处理成功",null).add("loginName",loginName);
    }else{
      return Message.fail("处理失败",null);
    }
  }

  /**
   * 主页面获取权限
   * @return json数据
   */
  @RequestMapping(value = "/getAuthority",method = RequestMethod.POST)
  public Message getAuthority(HttpServletRequest httpServletRequest){
    //获取Session域中的loginName值
    //就是数据库中的loginacct值
    String loginacct=(String)httpServletRequest.getSession().getAttribute("loginName");
    List<TablePermission> tablePermissionList=userService.getAllAuthority(loginacct);
    return Message.success("处理成功",null).add("permissionList",tablePermissionList);
  }

  /**
   * 获取所有用户信息
   * @param currentPage 当前页面
   * @return 当前页面的所有用户信息
   */
  @RequestMapping(value = "/getAllUser",method = RequestMethod.GET)
  public Message getAllUser(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage){
    //设置每页的数据量
    PageHelper.startPage(currentPage,10);
    //获取所有用户
    List<TableUser> tableUserList=userService.getAllUser();
    //传入数据，传入连续显示的页数
    PageInfo pageInfo=new PageInfo(tableUserList,5);
    Map<String,Object> searchBoolean=new HashMap<>(5);
    searchBoolean.put("searchFlag",false);
    return Message.success("处理成功",searchBoolean).add("pageInfo",pageInfo);
  }


  /**
   * 查询用户
   * @param searchInformation 查询信息
   * @param currentPage 当前页面
   * @return 查询到的用户信息
   */
  @RequestMapping(value = "/searchUser",method = RequestMethod.GET)
  public Message searchUserByCriteria(@RequestParam(value = "searchInformation",defaultValue = "") String searchInformation
   ,@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage){
    //设置每页数据量
    PageHelper.startPage(currentPage,10);
    //获取查询到的用户信息
    List<TableUser> tableUserList=userService.searchUserByCriteria(searchInformation);
    //传入数据，传入连续显示的页数
    PageInfo pageInfo=new PageInfo(tableUserList,5);
    Map<String,Object> searchBoolean=new HashMap<>(5);
    searchBoolean.put("searchFlag",true);
    return Message.success("处理成功",searchBoolean).add("pageInfo",pageInfo);
  }

  /**
   * 单个和批量删除用户
   * @param userDeleteIdString 要删除的用户的id字符串
   * @return 处理信息
   */
  @RequestMapping(value = "/delete/{userDeleteIdString}", method = RequestMethod.DELETE)
  public Message deleteUser(@PathVariable String userDeleteIdString) {
    boolean flag;
    if (userDeleteIdString.contains("-")) {
      List<Integer> deleteIdList = new ArrayList<>(10);
      String[] deleteIdArray = userDeleteIdString.split("-");
      for (String deleteId : deleteIdArray) {
        Integer userId = Integer.parseInt(deleteId);
        deleteIdList.add(userId);
      }
      flag = userService.batchDeleteUser(deleteIdList);
    } else {
      flag = userService.deleteUserById(Integer.parseInt(userDeleteIdString));
    }
    if (flag) {
      return Message.success("处理成功", null);
    } else {
      return Message.fail("处理失败", null);
    }
  }

  /**
   * 更新用户通过id
   * @param tableUser 前台传入的用户
   * @return 处理信息
   */
  @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT)
  public Message updateUser(TableUser tableUser){
    userService.updateUser(tableUser);
    return Message.success("处理成功",null);
  }
}
